package logic

import (
	"context"
	"github.com/google/uuid"
	"member_srv/models"
	"member_srv/pkg"

	"member_srv/internal/svc"
	"member_srv/member"

	"github.com/zeromicro/go-zero/core/logx"
)

type CreateMemberOrderLogic struct {
	ctx    context.Context
	svcCtx *svc.ServiceContext
	logx.Logger
}

func NewCreateMemberOrderLogic(ctx context.Context, svcCtx *svc.ServiceContext) *CreateMemberOrderLogic {
	return &CreateMemberOrderLogic{
		ctx:    ctx,
		svcCtx: svcCtx,
		Logger: logx.WithContext(ctx),
	}
}

func (l *CreateMemberOrderLogic) CreateMemberOrder(in *member.CreateMemberOrderRequest) (*member.CreateMemberOrderResponse, error) {
	// todo: add your logic here and delete this line
	var u models.User
	u.ID = uint(in.UserId)
	err := u.ShowUserById()
	if err != nil {
		logx.Error("账号不存在", err)
		return nil, err
	}

	var m models.Member
	m.ID = uint(in.MemberId)
	err = m.ShowMember()
	if err != nil {
		logx.Error("会员套餐不存在", err)
		return nil, err
	}

	var o models.MemberOrder
	o.UserId = int(in.UserId)
	o.MemberId = int(in.MemberId)
	o.OrderOn = uuid.NewString()
	o.PayType = 0
	err = o.Createorder()
	if err != nil {
		logx.Error("会员充值失败", err)
		return nil, err
	}

	alipay := pkg.Alipay(o.OrderOn, m.Price)
	return &member.CreateMemberOrderResponse{
		OrderOn: o.OrderOn,
		PayUrl:  alipay,
	}, nil
}
